﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using DevEnverModal;
using QuailDashboard.View.Helpers;
using QuailDashboard.View.Models;
using QuailDashboard.View.QuailServiceReference;

namespace QuailDashboard.View.Controls
{
    public partial class SelectEnvironmentControl
    {
        private readonly ModalControl _modalControl = new ModalControl();
        private SelectEnvironmentPageState _pageState = new SelectEnvironmentPageState();

        public SelectEnvironmentControl()
        {
            InitializeComponent();
            _pageState.PropertyChanged += OnPageStateChanged;
        }

        /// <summary>
        /// Gets or sets the ModalControl manager for this instance
        /// </summary>
        public ModalControl Modal
        {
            get;
            set;
        }

        public TestEnvironment SelectedTestEnvironment
        {
            get
            {
                return ListEnvironments.SelectedItem as TestEnvironment;
            }
        }

        public event EventHandler Select;

        private void OnPageStateChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ShowAllEnvironments")
                LoadEnvironments();
        }

        private void LoadEnvironments()
        {
            var service = QuailDataServiceFactory.CreateClient();

            if (_pageState.ShowAllEnvironments)
            {

                service.GetAllEnvironmentsCompleted += OnGetAllEnvironments;
                service.GetAllEnvironmentsAsync();
            }
            else
            {
                service.GetEnvironmentsCompleted += OnGetEnvironments;
                service.GetEnvironmentsAsync();
            }

            _pageState.IncProcessing("Loading...");
        }

        private void OnGetAllEnvironments(object sender, GetAllEnvironmentsCompletedEventArgs e)
        {
            if (_pageState == null)
                return;

            _pageState.DecProcessing();
            ListEnvironments.ItemsSource = e.Result;
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutRoot.DataContext = _pageState;
            LoadEnvironments();
        }

        private void OnGetEnvironments(object sender, GetEnvironmentsCompletedEventArgs e)
        {
            if (_pageState == null)
                return;

            _pageState.DecProcessing();
            ListEnvironments.ItemsSource = e.Result;
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (Select != null)
                Select(this, EventArgs.Empty);
            HideMe();
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            HideMe();
        }

        private void HideMe()
        {
            _pageState = null;
            Modal.HideModal();
        }

        private void ButtonConfigure_Click(object sender, RoutedEventArgs e)
        {
            var env = SelectedTestEnvironment;

            if (env == null)
                return;

            var ctrl = new AddEnvironmentControl
                           {
                               Modal = _modalControl,
                               Environment = env
                           };
            ctrl.Finished += OnFinished;
            _modalControl.ShowModal(ctrl);
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            var ctrl = new AddEnvironmentControl
                           {
                               Modal = _modalControl
                           };
            ctrl.Finished += OnFinished;
            _modalControl.ShowModal(ctrl);
        }

        private void OnFinished(object sender, EventArgs e)
        {
            LoadEnvironments();
        }

        public class SelectEnvironmentPageState : IsProcessingStateModel
        {
            private bool _showAllEnvironments;

            /// <summary>
            /// Gets or sets a value indicating whether to show all environments.
            /// </summary>
            public bool ShowAllEnvironments
            {
                get
                {
                    return _showAllEnvironments;
                }

                set
                {
                    if (value != _showAllEnvironments)
                    {
                        _showAllEnvironments = value;
                        NotifyPropertyChanged("ShowAllEnvironments");
                    }
                }
            }
        }
    }
}
